package com.me.controllers;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.me.bean.AjaxResp;
import com.me.entity.Groups;
import com.me.entity.RecentChat;
import com.me.entity.UserInfo;
import com.me.service.GroupService;
import com.me.service.RecentChatService;
import com.me.service.UserInfoService;
import com.me.service.WebSocketHandler;
import com.me.util.GetTIme;
import com.me.util.MapUtil;

@Controller
public class RecentChatController {

	@Autowired
	UserInfoService userInfoService;
	@Autowired
	RecentChatService recentChatService;
	@Autowired
	GroupService groupService;

	@RequestMapping("/add_recent")
	@Transactional
	@ResponseBody
	public AjaxResp addRecent(String account,String chat_tip,String type){
		RecentChat rc=new RecentChat();
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(false);
		UserInfo ui=null;
		int chat_tip_id=000;
		int uid=userInfoService.getUserIdByAccount(account);
		if("group".equals(type)){
			rc.setFid(Integer.parseInt(chat_tip));
			rc.setType(type);
		}else{
			 ui=(UserInfo)userInfoService.getUserByAccount(chat_tip).get(0);
			
			rc.setFid(ui.getId());
			rc.setType("user");
		}
		rc.setUid(uid);
		
		
		rc.setCtime(GetTIme.getNowTime());
		
		try {
			recentChatService.save(rc);
			ar.setSuccess(true);
			ar.setData(rc);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if("single".equals(type)){
			
			chat_tip_id=ui.getId();
			
			ar.setData(ui);
		}
		if("group".equals(type)){
			ui=new UserInfo();
			
			chat_tip_id=Integer.parseInt(chat_tip);
			Groups group=(Groups)groupService.getGroupInfoByGroupId(chat_tip_id).get(0);
			
			ar.setData(group);
		}
		
		
		return ar;
		
	}
	
	@RequestMapping("/getFriList_recent")
	@Transactional
	@ResponseBody
	public AjaxResp getRecentFriList(String account){
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(false);
		
		int uid=userInfoService.getUserIdByAccount(account);
		//得到最近联系人结果集，将结果集数据进行翻译
		List result=recentChatService.getRecentListByUid(uid+"");
		ar.setSuccess(true);
		
		List<Map<Object, Object>> list=MapUtil.arrayToList(result.size(), 3, result,new String[]{"name","tip","type"} );
		ar.setData(list);
		return ar;
	}
	
	@RequestMapping("/create_chat")
	@ResponseBody
	public AjaxResp createSingleChat(String account,String friaccount) throws Exception{
		//添加单聊联系人
		AjaxResp ar=new AjaxResp();
		RecentChat rc=new RecentChat();
		rc.setCtime(GetTIme.getNowTime());
		int fid=userInfoService.getUserIdByAccount(friaccount);
		int uid=userInfoService.getUserIdByAccount(account);
		rc.setFid(fid);
		rc.setUid(uid);
		rc.setType("user");
		
		recentChatService.save(rc);
		//保存成功，向客户端返回用户信息
		UserInfo ui=(UserInfo) userInfoService.getUserByAccount(friaccount).get(0);
		
		ar.setData(ui);
		
		ar.setSuccess(true);
		ar.setSuccess(false);
		return ar;
	}
	
	@RequestMapping("delete_recent_chat")
	@ResponseBody
	@Transactional
	public AjaxResp delete_recent_chat(String account,String friaccount,String type){
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(false);
		
		RecentChat rc=null;
		int uid=userInfoService.getUserIdByAccount(account);
		int fid=0;
		if("user".equals(type)){
			fid=userInfoService.getUserIdByAccount(friaccount);
			//删除user
			List list=recentChatService.getRecent(uid, fid, type);
			if(list.size()==0 || list.isEmpty()){
				//ar.setSuccess(false);
				return ar;
			}
			rc=(RecentChat)list.get(0);
			
			
		}else{
			//删除group
			fid=Integer.parseInt(friaccount);
			List list=recentChatService.getRecent(uid, fid, type);
			if(list.size()==0 || list.isEmpty()){
				return ar;
			}
			rc=(RecentChat)list.get(0);
		}
		recentChatService.delete(rc);
		ar.setData(rc);
		ar.setSuccess(true);
		
		
		return ar;
	}
	
	
}
